import 'package:cms/pages/home/home_state/home_state_cubit.dart';
import 'package:cms/pages/home/home_state/home_state_state.dart';
import 'package:cms/views/list_views/single_list_view.dart';
import 'package:cms/state/lara_press_cms/lara_press_cms_cubit.dart';
import 'package:cms/state/lara_press_cms/lara_press_cms_state.dart';
import 'package:common/export/bloc.dart';
import 'package:components/lara_press_app_bar.dart';
import 'package:components/lara_press_box.dart';
import 'package:components/lara_press_tabs.dart';
import 'package:components/lara_press_text.dart';
import 'package:components/keep_alive_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:common/export/tools.dart';

class CmsHomeTabs extends StatefulWidget {
  const CmsHomeTabs({super.key});

  @override
  State<CmsHomeTabs> createState() => _CmsHomeTabsState();
}

class _CmsHomeTabsState extends State<CmsHomeTabs>
    with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<LaraPressCmsCubit, LaraPressCmsState>(
      builder: (context, state) {
        if (state.configuration == null) {
          return const SizedBox.shrink();
        }
        return BlocProvider(
          create: (BuildContext context) =>
              HomeStateCubit(tickerProvider: this),
          child: Builder(builder: (context) => _buildPage(context)),
        );
      },
    );
  }

  Widget _buildTab(BuildContext context, HomeStateState state) {
    if (state.tabController == null) {
      return const SizedBox.shrink();
    }
    return Column(
      children: [
        if (state.tabController != null)
          LaraPressEmptyBox(
            child: SizedBox(
              height: 30.h,
              width: double.infinity,
              child: LaraPressTabBar(
                controller: state.tabController!,
                tabAlignment: TabAlignment.start,
                isScrollable: true,
                tabs: state.homeShowCategory != null
                    ? List<String>.from(
                        state.homeShowCategory!.map((e) => "${e.name}"),
                      )
                    : [],
              ),
            ),
          ),
        Expanded(
          child: TabBarView(
            controller: state.tabController,
            children:
                state.homeShowCategory?.map((e) {
                  return LaraPressKeepAliveWrapper(
                    page: LaraPressSingleListView(categoryId: e.id!),
                  );
                }).toList() ??
                [],
          ),
        ),
      ],
    );
  }

  Widget _buildPage(BuildContext context) {
    return BlocBuilder<HomeStateCubit, HomeStateState>(
      builder: (context, state) {
        return Scaffold(
          appBar: LaraPressAppBar(
            titleWidget: LaraPressBox(
              backgroundColor: Theme.of(
                context,
              ).colorScheme.surfaceContainerLow,
              padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.h),
              borderRadius: BorderRadius.circular(10.r),
              width: double.infinity,
              onTap: () {
                context.pushNamed('cms-search');
              },
              child: Row(
                children: [
                  Expanded(
                    child: LaraPressText(
                      text: '搜索',
                      type: LaraPressTextType.listTitle,
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                  ),
                  Icon(
                    Icons.search,
                    color: Theme.of(context).colorScheme.onSurfaceVariant,
                  ),
                ],
              ),
            ),
          ),
          body: _buildTab(context, state),
        );
      },
    );
  }
}
